<?php
    class Model_User extends CI_Model{
        private $logged_in=false;
        public $user_id;
        public $user_name;
        public $password;

        function __construct() {
        //$this->check_message();
            $this->check_login();
            if($this->logged_in){
            ////
            }else {
             ///
            }
        }
        public function delete_student($student_id){

            $this->db->delete('student',array('student_id'=>$student_id));
            if($this->db->affected_rows()!=0){
                return true;
            }else{
                 return false;
            }
        }
        
        public function user_info($user_id){

            $this->db->select('user_id,user_name');
            $this->db->where('user_id', $user_id);
            $query=$this->db->get('users');
           if($query->num_rows($query)>0){
               $row=$query->row();
               return $row;
           } 
       //    return $row;
        }

        public function is_logged_in(){
            return $this->logged_in;
        }

        public function login($user_id) {
 
            if($user_id){
                $this->user_id=$user_id;
                $this->session->set_userdata('user_id',$user_id);
                echo $this->user_id=$user_id;
                $this->logged_in = true;
            }
        }
       
        
        public function logout() {
            $this->session->unset_userdata(user_id);
            unset($this->user_id);
            $this->logged_in= false;
            redirect("user/login");
        }
        
        public function message($msg="") {
            if(!empty($msg)){
	
                
        }
        else{
	    // then this is "get message"
            return $this->message;
	        }
        }
        public function authenticate($user_name="",$password=""){

            $user_name = $this->db->escape($user_name);
            $query = $this->db->query($this->authenticate_sql($user_name,$password));
            $query=$query->result_array($query);
            if(empty($query)){
                echo no;
                return false;
            }else{
            $query_array=$query[0];
            return !empty($query_array) ? array_shift($query_array) : false;
            }
        }

        public function add_new_student($student_id,$student_name,$nick_name,$password,$sex,$birthday,$phone_number,$email){

            $data=array('student_id'=>$student_id,
                    'student_name'=>$student_name,
                    'nick_name'=>$nick_name,
                    'password'=>$password,
                    'sex'=>$sex,
                    'birthday'=>$birthday,
                    'phone_number'=>$phone_number,
                    'email'=>$email
                );
            $this->db->insert('student',$data);
            if($this->db->affected_rows()!=0){
                return true;
            }else{
                 return false;
            }
        }
        public function show_all_student(){

            $this->db->select('student_id,student_name,nick_name,sex,birthday,phone_number,email');
            $query=$this->db->get('student');
            return $query;
        }
        public function show_one_student($student_id){

            $query=$this->db->get_where('student',array('student_id'=>$student_id));
            return $query;
        }
        
        public function update_student($student_id,$phone_number,$email){

            $data=array('student_id'=>$student_id,
                    'phone_number'=>$phone_number,
                    'email'=>$email
                );
            $this->db->where('student_id',$student_id);
            $this->db->update('student',$data);
            if($this->db->affected_rows()!=0){
                return true;
            }else{
                 return false;
            }
        }
            
        private function check_login(){
            if($this->session->userdata("user_id")){
                $this->user_id=$this->session->userdata("user_id");
                $this->logged_in=true;
                }else{
                    unset($this->user_id);
                $this->logged_in = false;
            }
        }
        
        private function authenticate_sql($user_name,$password){
            $sql = "SELECT * ";
            $sql .="FROM wse.users"; 
            $sql .=" WHERE user_name={$user_name}"; 
            $sql .="AND password = {$password} ";
            $sql .="LIMIT 1";
            return $sql;
        }
    }
?>
